<template>
  <div>
    <el-dialog
      :title="isEdit ? '修改学科' : '新增学科'"
      :visible="dialogFormVisible"
      @close="close"
    >
      <el-form :model="form" :rules="rules">
        <el-form-item
          label="学科名称"
          :label-width="formLabelWidth"
          prop="subjectName"
        >
          <el-input v-model="form.subjectName"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="是否显示">
          <el-switch
            v-model="form.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="active"
            :inactive-value="inactive"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="close">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects'
export default {
  name: 'subjectsDia',
  data () {
    return {
      active: 1,
      inactive: 2,
      formLabelWidth: '80px',
      value: '100',
      form: {
        subjectName: '',
        isFrontDisplay: 1
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    dialogFormVisible: {
      type: Boolean
    },
    isEdit: {
      type: Boolean
    },
    dataEdit: {
      type: Object
    }
  },
  created () {
    // this.dataEdit.isFrontDisplay = this.dataEdit.isFrontDisplay + ''
    this.form = JSON.parse(JSON.stringify(this.dataEdit))
  },
  computed: {},
  methods: {
    onSubmit () {
      this.isEdit ? this.doEdit() : this.doAdd()
      this.$emit('success')
      this.close()
    },
    async doAdd () {
      await add(this.form)
      this.$message.success('新增成功!')
    },
    async doEdit () {
      this.form.id = this.dataEdit.id
      await update(this.form)
      this.$message.success('修改成功!')
    },
    close () {
      this.$emit('update:dialogFormVisible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  width: 400px;
}
</style>
